package com.gtmc.ecm.common.core.utils;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @description: zip操作工具类
 * @author: zhz
 * @create: 2021/12/24 11:06
 */
public class ZipUtil {

    private static final int BUFFER_SIZE = 2 * 1024;

    /**
     * 压缩成ZIP
     *
     * @param srcFiles 需要压缩的文件列表
     * @param out      压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[BUFFER_SIZE];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("文件压缩完成，耗时：" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 对.zip文件进行解压缩
     *
     * @param fileName 解压缩文件
     * @return java.util.List<java.io.File>
     * @Date 2021/12/24
     */
    public static List<File> unzipFile(String fileName) {
        List<File> list = new ArrayList<>();
        try {
            File file = new File(fileName);
            ZipFile zipFile = new ZipFile(file);
            ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                File outFile = new File(entry.getName());
                BufferedInputStream bis = new BufferedInputStream(
                        zipFile.getInputStream(entry));
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(outFile));
                byte[] b = new byte[100];
                while (true) {
                    int len = bis.read(b);
                    if (len == -1)
                        break;
                    bos.write(b, 0, len);
                }
                bis.close();
                bos.close();
                list.add(outFile);
            }
            zipFile.close();
            zis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
